//
//  ZhuanTiCollectionViewCell.m
//  礼物说
//
//  Created by dllo on 16/4/26.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "ZhuanTiCollectionViewCell.h"

@interface ZhuanTiCollectionViewCell()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) NSMutableArray * zhongZhuanArr;

@end


@implementation ZhuanTiCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        [self createCollection];
    }
    return self;
}
- (void)dealloc{
    
    [_zhongZhuanArr release];
    [_zhuanTiArr release];
    [_collecitonView release];
    [super dealloc];
}

- (void)setZhuanTiArr:(NSMutableArray *)zhuanTiArr{
    if (_zhuanTiArr != zhuanTiArr) {
        [_zhuanTiArr release];
        _zhuanTiArr = [zhuanTiArr retain];
    }
    
    self.zhongZhuanArr = [NSMutableArray arrayWithArray:_zhuanTiArr];
}
- (void)createCollection{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = kWIDTHS(10);
    layout.itemSize = CGSizeMake(kWIDTHS(150), kHEIGHTCELL);
    layout.sectionInset = UIEdgeInsetsMake(0, kWIDTHS(10), 0, kWIDTHS(10));
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collecitonView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTHCELL, kHEIGHTCELL) collectionViewLayout:layout];
//    _collecitonView.backgroundColor = [UIColor whiteColor];
    _collecitonView.pagingEnabled = NO;
    [self.contentView addSubview:_collecitonView];
    [_collecitonView release];
    [layout release];
    
    
    _collecitonView.delegate = self;
    _collecitonView.dataSource = self;
    
    
    [_collecitonView registerClass:[ZhuanTicellCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _zhongZhuanArr.count;
}
- (UICollectionViewCell * )collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ZhuanTicellCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model = _zhongZhuanArr[indexPath.row];
    
    return cell;
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ClassifyGongLvViewController * gongLvVc = [[ClassifyGongLvViewController alloc] init];
    
    ZhuanTiModel * model = _zhongZhuanArr[indexPath.row];
    
    gongLvVc.num = model.number;
    gongLvVc.titles = model.title;
    gongLvVc.Judge = @"zhuanTi";
    UIViewController * ViewController = [self viewController];
    
    [ViewController.navigationController pushViewController:gongLvVc animated:YES];
    [gongLvVc release];
    
}
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}



@end
